package com.example.last.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.last.common.enums.ErrorCode;
import com.example.last.dao.AdminDao;
import com.example.last.dao.AuditDao;
import com.example.last.entity.*;
import com.example.last.exception.CustomException;
import com.example.last.utils.JwtTokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AuditService {
    @Resource
    private AuditDao AuditDao;
    @Resource
    private AdminDao adminDao;

    public PageInfo<Audit> findBySearch(@Param("params") Params params){
        Account user = JwtTokenUtils.getCurrentUser();
        if(ObjectUtil.isEmpty(user)){
            throw new CustomException(ErrorCode.TOKEN_CHECK_ERROR);
        }
        if("ROLE_STUDENT".equals(user.getRole())){
            params.setUserId(user.getId());
        }
        //开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        List<Audit> list =  AuditDao.findBySearch(params);
        return PageInfo.of(list);
    }

    public void add(Audit Audit){
        AuditDao.add(Audit);
    }

    public void update(Audit Audit){
        AuditDao.update(Audit);
    }

    public void delete(Integer id){
        AuditDao.delete(id);
    }


}
